import request from "../../utils/request"
import { login } from "../../utils"
import store from "../index"

export const userActionType = {
  getters: {
    getUserInfo: "user/getUserInfo",
    shareUserId: "user/shareUserId",
    examInfo: "user/examInfo",
    recommender: {
      id: "user/recommender/id",
      behavior: "user/recommender/behavior",
    },
    channelId: "user/channelId",
    userFrom: "user/userFrom",
    userSelectExamId: "user/userSelectExamId",
    userSelectExamName: "user/userSelectExamName",
    userSelectExamTime: "user/userSelectExamTime",
    XWX_schoolName: "user/XWX_schoolName",
  },
  mutations: {
    setUserInfo: "user/setUserInfo",
    shareUserId: "user/shareUserId",
    examInfo: "user/examInfo",
    recommender: {
      id: "user/recommender/id",
      behavior: "user/recommender/behavior",
    },
    channelId: "user/channelId",
    userFrom: "user/userFrom",
    userSelectExamId: "user/userSelectExamId",
    userSelectExamName: "user/userSelectExamName",
    userSelectExamTime: "user/userSelectExamTime",
    XWX_schoolName: "user/XWX_schoolName",
  },
  action: {
    examInfo: "user/examInfo",
    userInfo: "user/userInfo",
    recommender: {
      behavior: "user/recommender/behavior",
    },
    refreshUserInfo: "user/refreshUserInfo",
  },
}

export default {
  namespaced: false,
  state: {
    userInfo: null,
    shareUserId: null,
    examInfo: null, //用户的考试信息
    recommender: {
      //推荐官相关
      id: "", //推荐官Id
      behavior: null, //推荐官行为
    },
    channelId: "", //渠道码
    userFrom: "", //用户来源（XWX_USER 校务行）
    userSelectExamId: "", // 用户选择的考试id
    userSelectExamName: "", // 用户选择的考试名称
    userSelectExamTime: "", // 用户选择的考试时间
    XWX_schoolName: "", // 校务行来源学校名称
  },
  getters: {
    //不要直接调用这里的getter，应该调用action
    [userActionType.getters.getUserInfo](state) {
      return state.userInfo
    },
    [userActionType.getters.shareUserId](state) {
      return state.shareUserId
    },
    //不要直接调用这里的getter，应该调用action
    [userActionType.getters.examInfo](state) {
      return state.examInfo
    },
    //推荐官相关
    [userActionType.getters.recommender.id](state) {
      return state.recommender.id
    },
    //不要直接调用这里的getter，应该调用action
    [userActionType.getters.recommender.behavior](state) {
      return state.recommender.behavior
    },
    [userActionType.getters.channelId](state) {
      return state.channelId
    },
    [userActionType.getters.userFrom](state) {
      return state.userFrom
    },
    [userActionType.getters.userSelectExamId](state) {
      return state.userSelectExamId
    },
    [userActionType.getters.userSelectExamName](state) {
      return state.userSelectExamName
    },
    [userActionType.getters.userSelectExamTime](state) {
      return state.userSelectExamTime
    },
    [userActionType.getters.XWX_schoolName](state) {
      return state.XWX_schoolName
    },
  },
  mutations: {
    [userActionType.mutations.setUserInfo](state, userInfo) {
      state.userInfo = userInfo
    },
    [userActionType.mutations.shareUserId](state, shareUserId) {
      state.shareUserId = shareUserId
    },
    [userActionType.mutations.examInfo](state, examInfo) {
      state.examInfo = examInfo
      if (examInfo) {
        state.userSelectExamId = examInfo.examId
        state.userSelectExamName = examInfo.examName
        state.userSelectExamTime = examInfo.examTime
      }
    },
    //推荐官相关
    [userActionType.mutations.recommender.id](state, id) {
      state.recommender.id = id
    },
    [userActionType.mutations.recommender.behavior](state, behavior) {
      state.recommender.behavior = behavior
    },
    [userActionType.mutations.channelId](state, channelId) {
      state.channelId = channelId
    },
    [userActionType.mutations.userFrom](state, userFrom) {
      state.userFrom = userFrom
    },
    [userActionType.mutations.userSelectExamId](state, userSelectExamId) {
      state.userSelectExamId = userSelectExamId
    },
    [userActionType.mutations.userSelectExamName](state, userSelectExamName) {
      state.userSelectExamName = userSelectExamName
    },
    [userActionType.mutations.userSelectExamTime](state, userSelectExamTime) {
      state.userSelectExamTime = userSelectExamTime
    },
    [userActionType.mutations.XWX_schoolName](state, XWX_schoolName) {
      state.XWX_schoolName = XWX_schoolName
    },
  },
  actions: {
    //获取用户选择的考试信息
    //配置项：jumpToSelectExamPage：如果用户没有选择考试，是否跳转到选择考试页面
    [userActionType.action.examInfo](store, params) {
      return new Promise(async (resolve, reject) => {
        request({
          url: "exam/getCurrentExam",
          showLoading: true,
        })
          .then(async (data) => {
            //用户还没有 选择考试，跳转到选择考试页面
            if (data) {
              store.commit(userActionType.mutations.examInfo, data)
            } else {
              params && params.jumpToSelectExamPage && uni.navigateTo({ url: "/pages/examSelection/index" })
            }
            resolve(store.getters[userActionType.getters.examInfo])
          })
          .catch((err) => {
            console.log(err, "err")
            reject(err)
          })
      })
    },
    //登录以获取用户信息
    [userActionType.action.userInfo](store) {
      return new Promise(async (resolve, reject) => {
        if (store.getters[userActionType.getters.getUserInfo]) {
          resolve(store.getters[userActionType.getters.getUserInfo])
        } else {
          login()
            .then((userInfo) => {
              store.commit(userActionType.mutations.setUserInfo, userInfo)
              resolve(store.getters[userActionType.getters.getUserInfo])
            })
            .catch((err) => {
              console.log(err, "err")
            })
            .finally(() => {})
        }
      })
    },
    //刷新用户信息
    [userActionType.action.refreshUserInfo](store) {
      return new Promise((resolve, reject) => {
        request({
          url: "authorize/refreshToken",
          showLoading: true,
          errorHandle: true,
        })
          .then((data) => {
            store.commit(userActionType.mutations.setUserInfo, data)
            resolve()
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
  },
}
